from fastapi import APIRouter, Request
from app.core.redis import redis_client

router = APIRouter(prefix="/redis", tags=["Redis 操作"])


@router.get("/set", response_model=dict)
async def set_redis_key(key: str, value: str, expire: int = 60):
    """设置 Redis 键值对"""
    await redis_client.set(key, value, ex=expire)
    return {"message": "OK"}


@router.get("/get", response_model=dict)
async def get_redis_key(key: str, request: Request):
    """获取 Redis 键值对"""
    value = await redis_client.get(key)
    return {"key": key, "value": value}


@router.delete("/delete", response_model=dict)
async def delete_redis_key(key: str, request: Request):
    """删除 Redis 键值对"""
    result = await redis_client.delete(key)
    if result == 1:
        return {"message": "Key deleted"}
    else:
        return {"message": "Key not found"}
